package com.towexin.application.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.towexin.common.ResultData;
import com.towexin.config.mylogging.LogType;
import com.towexin.config.mylogging.MyLog;
import com.towexin.application.system.domain.Authority;
import com.towexin.application.system.domain.AuthorityMenu;
import com.towexin.application.system.service.AuthorityMenuService;
import com.towexin.application.system.service.AuthorityService;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * TODO
 *
 * @author 唐伟鑫
 * @version 1.0
 * @date 2021/4/12 18:33
 */
@Api(tags = "权限管理接口")
@RestController
@RequestMapping("authority")
public class AuthorityController {
    @Autowired
    private AuthorityService service;

    @Autowired
    private AuthorityMenuService authorityMenuService;

    @ApiOperation("增加新权限")
    @MyLog(type = LogType.INSERT, operation = "增加权限")
    @PostMapping("save")
    @RequiresPermissions("system.authority.add")
    public ResultData saveOrUpdate(Authority authority) {
        List<Authority> oldAuthors = service.list(new QueryWrapper<Authority>().eq("authority_mark", authority.getAuthorityMark()));

        if (oldAuthors.size()>0) {
            return ResultData.NO("权限标识重复");
        }
        return service.saveOrUpdateAuthority(authority) ? ResultData.OK() : ResultData.NO();
    }

    @ApiOperation("修改权限信息")
    @MyLog(type = LogType.UPDATE, operation = "修改权限信息")
    @PostMapping("update")
    @RequiresPermissions("system.authority.edit")
    public ResultData update(Authority authority){
        List<Authority> oldAuthors = service.list(new QueryWrapper<Authority>().eq("authority_mark", authority.getAuthorityMark()));

        if (oldAuthors.size()>0 && !oldAuthors.get(0).getId().equals(authority.getId())) {
            return ResultData.NO("权限标识重复");
        }
        authority.setAuthorityMark(null);
        return service.saveOrUpdateAuthority(authority) ? ResultData.OK() : ResultData.NO();
    }

    //@MyLog(type = 1, operation = "获取权限列表")
    @GetMapping("selectAll")
    @RequiresPermissions("system.authority.list")
    public ResultData selectAll() {
        return ResultData.OK(service.selectAllMap());
    }

    // 获取上级权限列表
    @GetMapping("selectTree")
    public ResultData selectAuthorityTree() {
        return ResultData.OK(service.selectAuthorityTree(true));
    }

    @MyLog(type = LogType.UPDATE, operation = "权限关联到菜单")
    @PostMapping("associate")
    @RequiresPermissions("system.authority.associate")
    public ResultData associateMenu(AuthorityMenu authorityMenu) {
        return authorityMenuService.associateMenu(authorityMenu) ? ResultData.OK() : ResultData.NO();
    }

    @MyLog(type = LogType.UPDATE, operation = "取消权限与菜单关联")
    @PostMapping("disAssociate")
    @RequiresPermissions("system.authority.associate")
    public ResultData disAssociateMenu(String aId){
        return authorityMenuService.removeById(aId)?ResultData.OK():ResultData.NO();
    }

    @GetMapping("checkMenu")
    public ResultData checkMenu(String pId) {
        String res = service.checkMenu(pId);
        return res.equals("请先关联父节点") ? ResultData.NO(res) : ResultData.OK(res);
    }

    @MyLog(type = LogType.DELETE, operation = "删除权限")
    @GetMapping("deleteById")
    @RequiresPermissions("system.authority.delete")
    public ResultData deleteById(String id) {
        return service.deleteCascadeId(id) ? ResultData.OK() : ResultData.NO();
    }

    @GetMapping("openStatus")
    public ResultData openStatus(String id, Boolean open){
        Authority authority = new Authority();
        authority.setId(id);
        authority.setOpen(open);
        return service.updateById(authority)?ResultData.OK():ResultData.NO();
    }
}
